package com.cn.itcast.gjp.controller;

import com.cn.itcast.gjp.domain.Ledger;
import com.cn.itcast.gjp.mapper.LedgerMapper;
import com.cn.itcast.gjp.mapper.SortMapper;
import com.cn.itcast.gjp.view.AbstractOperationLedgerDialog;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AddLedgerController extends AbstractOperationLedgerDialog {
    public AddLedgerController(JDialog dialog) {
        super(dialog);
        SqlSession session= MyBatisUtil.getSqlSession();
        LedgerMapper ledgerMapper = session.getMapper(LedgerMapper.class);
        SortMapper sortMapper = session.getMapper(SortMapper.class);
        session.close();
    }

    @Override
    public void changeParent() {
        SqlSession session= MyBatisUtil.getSqlSession();
        SortMapper sortMapper = session.getMapper(SortMapper.class);


        String parent = this.parentBox.getSelectedItem().toString();
        if(parent.equals("-请选择-")  ) {
            Object[] sortItems = {"-请选择-"};
            sortBox.setModel(new DefaultComboBoxModel(sortItems));
        } else   {
            List<String> nameItems = sortMapper.getSnameByParent(parent);
            nameItems.add(0, "-请选择-") ;
            sortBox.setModel(new DefaultComboBoxModel(nameItems.toArray())) ;
        }

        session.close();
    }

    @Override
    public void confirm() throws ParseException {
        String parent = parentBox.getSelectedItem().toString();
        if(parent.equals("-请选择-")) {
            JOptionPane.showMessageDialog(this, "\"收/支\"不能为空！");
            return;
        }
        String sname = sortBox.getSelectedItem().toString();
        if(sname.equals("-请选择-")) {
            JOptionPane.showMessageDialog(this, "\"分类\"不能为空！");
            return;
        }
        String account = accountTxt.getText();
        if(account.isEmpty()) {
            JOptionPane.showMessageDialog(this, "\"账户\"不能为空！");
            return;
        }
        double money = 0;
        try {
            money = Double.parseDouble(moneyTxt.getText());
        } catch(NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "\"账户\"不能为空！");
            return;
        }

        //数据库操作
        SqlSession session = MyBatisUtil.getSqlSession();
        LedgerMapper ledgerMapper= session.getMapper(LedgerMapper.class);
        SortMapper sortMapper = session.getMapper(SortMapper.class);

        String createtime = createtimeTxt.getText();
        String desc = ldescTxt.getText();
        Ledger ledger = new Ledger();
        ledger.setParent(parent);

        ledger.setAccount(account);

        ledger.setMoney(money);

        ledger.setCreatetime(createtime);

        ledger.setLdesc(desc);


        int sid = sortMapper.getSidBySname(sname);
        ledger.setSid(sid) ;


        ledgerMapper.AddLedger(ledger);
        this.dispose();
        session.commit();
        session.close();
    }
}
